<?php
	include_once('db/config.php');
	
	class category
	{
		public $code;
		public $name;
		public $description;
		
		public function __contructor()
		{
			$this->code = 0;
			$this->name = '';
			$this->description = '';
		}
		
		public function setCategory($categoryCode='', $categoryName='', $description='')
		{
			$this->code = $categoryCode;
			$this->name = $categoryName;
			$this->description = $description;;
		}
		
		public function retrieveCategory()
		{
			try
			{
				connectDb("everythingdb");
				global $dbLink;
				$resultPointer = mysqli_query($dbLink,"");
				if($resultPointer)
				{
					while($resultRow = mysqli_fetch_array($resultPointer))
					{
						//
					}
					return true;
				}
				else
				{
					return false;
				}
			}
			catch(Exception $ex)
			{
				throw new Exception($ex->getMessage());
			}
		}
		
		public function searchCategory($categoryCode='', $categoryName='')
		{
			try	
			{
				connectDb("everythingdb");
				global $dbLink;
				$partialQueryString1 = '';
				$partialQueryString2 = '';
				$partialQueryString3 = '';
				$whereClause='';
				if(!empty($categoryCode))
				{
					$partialQueryString1.= " category_category.category_code = ".$categoryCode; 
				}
				if(!empty($categoryName))
				{
					if(!empty($categoryCode))
					{
						$partialQueryString2.= ' and category.name = "'.$categoryName.'"';
					}
					else
					{
						$partialQueryString2.= ' category.name = "'.$categoryName.'"';				
					}
				}
				if(!empty($categoryCode))
				{
					if(!empty($categoryCode) || !empty($categoryName))
					{
						$partialQueryString3.= " and category.code = ".$categoryCode;
					}
					else
					{
						$partialQueryString3.= " category.code = ".$categoryCode;			
					}
					
				}
				if(!empty($partialQueryString1) || !empty($partialQueryString2) || !empty($partialQueryString3))
				{
					$whereClause= ' where ';
				}				
				
				$queryString = "select category.code,category.name,category.description,category.unit_price,category.on_hand from category inner join category_category on category.code = category_category.category_code ".$whereClause.$partialQueryString1.$partialQueryString2.$partialQueryString3;
				
				$resultPointer = mysqli_query($dbLink,$queryString);
				if($resultPointer)
				{
					$resultTable = array();
					while($resultRow = mysqli_fetch_assoc($resultPointer))
					{
						array_push($resultTable, $resultRow);
					}
					return $resultTable;
				}
				else
				{
					return false;
				}
			}
			catch(Exception $ex)
			{
				throw new Exception($ex->getMessage());
			}
		}	
		
		public function addCategory()
		{
			try
			{
				connectDb("everythingdb");
				global $dbLink;
				$queryString = "call sp_addCategory('".mysqli_real_escape_string($dbLink,$this->name)."','".mysqli_real_escape_string($dbLink,$this->description)."')";
				//echo $queryString;
				$resultPointer = mysqli_query($dbLink,$queryString);
				//echo $resultPointer;
				if($resultPointer)
				{
					return true;
				}
				else
				{
					return false;
				}
			}
			catch(Exception $ex)
			{
				throw new Exception($ex->getMessage());
			}
		}
		
		
		public function deleteCategory()
		{
			try
			{
				connectDb("everythingdb");
				global $dbLink;
				$queryString = "delete from category where code = ".mysql_real_escape_string($this->code);
				//echo $queryString;
				$resultPointer = mysqli_query($dbLink,$queryString);
				//echo $resultPointer;
				if($resultPointer)
				{
					return true;
				}
				else
				{
					return false;
				}
			}
			catch(Exception $ex)
			{
				throw new Exception($ex->getMessage());
			}
		}
		
		
		function updateCategory()
		{
			return true;
		}
	
	
		public function getAllCategory()
		{
			try
			{	
				connectDb("everythingdb");
				
				global $dbLink;
				
				$resultPointer = mysqli_query($dbLink,"call sp_getAllCategory()");
				if($resultPointer)
				{	
					$resultTable = array();
					while($resultRow = mysqli_fetch_array($resultPointer))
					{
						array_push($resultTable, $resultRow);
					}
					return $resultTable;
				}
				else
				{
					return false;
				}
			}
			catch(Exception $ex)
			{
				throw new Exception($ex->getMessage());
			}
		}
	}
?>